package com.gxa.order.service.impl;

import com.gxa.common.commons.R;
import com.gxa.order.feign.ServiceOrderApi;
import com.gxa.order.mapper.HotelManageMapper;
import com.gxa.order.service.HotelManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;

@Service
public class HotelManageServiceImpl implements HotelManageService {

    @Autowired
    private HotelManageMapper hotelManageMapper;
    @Autowired
    private ServiceOrderApi serviceOrderApi;

    //同意退款申请
    @Override
    public R agreeExamine(Integer id) {
        R r = new R();
        try{
            Integer statu = 6;
//            int i = hotelManageMapper.agreeExanmine(id);
            R updatestatu = serviceOrderApi.updatestatu(statu, id);

            r.setCode(200);
            r.setMsg("审核通过");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("审核失败");
        }
        return r;
    }

    //拒绝退款申请
    @Override
    public R notAgreeExamine(Integer id) {
        R r = new R();
        try{
//            int i = hotelManageMapper.notAgreeExamine(id);
            R updatestatu = serviceOrderApi.updatestatu(5, id);
            r.setCode(200);
            r.setMsg("拒绝退款申请成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差");
        }
        return r;
    }

    //换房
    @Override
    public R exchangeRoom(HashMap map) {
        R r = new R();
        try {
            //设置时区
            TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
            TimeZone.setDefault(timeZone);
            //获取当前时间
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(date);
            map.put("into_date",format);
            //更改原房间
            int result = hotelManageMapper.updateOldRoom(map);
            //修改新的房间
            int i = hotelManageMapper.updateNewRomm(map);
            //修改住客表的房间号
            int o = hotelManageMapper.updateTenantRoom(map);
            //修改订单内容修改房间号和房间id
            serviceOrderApi.updateRoom(Integer.parseInt(map.get("id").toString()),Integer.parseInt(map.get("newId").toString()),(String) map.get("room_number"));
            r.setCode(200);
            r.setMsg("换房成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，换房失败");
        }
        return r;
    }

    //退房
    @Override
    public R checkOut(HashMap map) {
        R r = new R();
        try {
            //修改房间状态和入住时间
            int result = hotelManageMapper.updateRoomStatusAndTime(map);
            //修改住客表的离店日期
            //设置时区
            TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
            TimeZone.setDefault(timeZone);
            //获取当前时间
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(date);
            map.put("leave_date",format);
            int i = hotelManageMapper.updateTenantLeaveDate(map);
            //退房
            serviceOrderApi.updatestatu(8,Integer.parseInt(map.get("id").toString()));
            r.setCode(200);
            r.setMsg("退房成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，退房失败");
        }

        return r;
    }

}
